package com.jt.tenant.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jt.tenant.R;
import com.jt.tenant.activity.BatteryStateActivity;
import com.jt.tenant.activity.CarInfoActivity;
import com.jt.tenant.activity.CarLocationActivity;
import com.jt.tenant.activity.ChooseModelsActivity;
import com.jt.tenant.activity.DetectionActivity;
import com.jt.tenant.activity.DetectionResultActivity;
import com.jt.tenant.activity.DiagnosisActivity;
import com.jt.tenant.activity.ErrorCodeActivity;
import com.jt.tenant.activity.ExpenseDetailActivity;
import com.jt.tenant.activity.IllegalQueryActivity;
import com.jt.tenant.activity.MaintaenanceSettingActivity;
import com.jt.tenant.adapter.ExpenseDetailsAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.conn.GetCarGps;
import com.jt.tenant.conn.GetExpenseDetail;
import com.jt.tenant.conn.GetMaintainRemind;
import com.jt.tenant.dialog.AddExpenseDialog;
import com.jt.tenant.widget.MyListView;
import com.jt.tenant.widget.Tools;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/2/2.
 */
public class CarFragment extends AppV4Fragment implements View.OnClickListener {

    private ImageView mCarImg;

    private TextView mCarNameText;

    private TextView mCarNumText;

    private TextView mCarInfoText;

    private ImageView mLocationImg;

    private TextView mEditCarInfo;

    private TextView mEngineNum;

    private TextView mVincode;

    private LinearLayout car_state;

    private TextView mReTime;

    private TextView mMiles;

    private TextView mMaintainPeriod;

    private TextView mBatteryState;

    private TextView mDetection;

    private LinearLayout mLinBattery;

    private LinearLayout mLinMaintain;

    private LinearLayout mLinPeccancy;

    private LinearLayout mLinClearError;

    private ImageView iv_car_battery, iv_car_clear_error;

    private TextView add_cost, cost_detail;

    private MyListView lv_car_cost;

    private ExpenseDetailsAdapter mExpAdapter;

    private List<GetExpenseDetail.ExpenseDetailList> mExpList = new ArrayList<>();

    private List<GetExpenseDetail.ExpenseDetailList> mShowList = new ArrayList<>();

    public static RefreshLinster refreshLinster;

    private String account_state;//账号状态   0：未添加车辆，未绑定设备  1：已添加车辆，未绑定设备  2：已绑定设备

    private GetMaintainRemind getMaintainRemind = new GetMaintainRemind(new AsyCallBack<GetMaintainRemind.Info>() {
        @Override
        public void onSuccess(String toast, int type, GetMaintainRemind.Info info) throws Exception {
            super.onSuccess(toast, type, info);
            mMiles.setText("优路兔已陪伴您的爱车 : " + Tools.F_num(info.TOTAL_MAINTENANCE_DISTANCE) + "公里");
            mMaintainPeriod.setText("设定保养周期 : " + info.time_space + "个月");

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);
        }
    });
    ;

    private GetCarGps getCarGps = new GetCarGps(new AsyCallBack<GetCarGps.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetCarGps.Info info) throws Exception {

            super.onSuccess(toast, type, info);

            startActivity(new Intent(getActivity(), CarLocationActivity.class)

                    .putExtra("longitude", info.lng + "")

                    .putExtra("latitude", info.lat + "")

                    .putExtra("location", "当前车辆位置")

            );
        }
    });

    private GetExpenseDetail getExpenseDetail = new GetExpenseDetail(new AsyCallBack<GetExpenseDetail.Info>() {

        @Override
        public void onSuccess(String toast, int type, GetExpenseDetail.Info info) throws Exception {
            if (info.msg.equals("000")) {

                mExpList.clear();

                mExpList.addAll(info.expenseDetailLists);

                mExpAdapter.notifyDataSetChanged();

            }
        }
    });

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = JTApplication.ScaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_car, null));

        initView(view);

        return view;

    }

    private void initView(View v) {

        account_state = JTApplication.jtPreferences.readAccountState();

        mCarImg = (ImageView) v.findViewById(R.id.img_car_flag);

        mCarNameText = (TextView) v.findViewById(R.id.text_car_flag);

        mCarInfoText = (TextView) v.findViewById(R.id.text_car_info);

        mCarNumText = (TextView) v.findViewById(R.id.text_car_num);

        mLocationImg = (ImageView) v.findViewById(R.id.icon_location);

        mEditCarInfo = (TextView) v.findViewById(R.id.text_editcarinfo);

        mEngineNum = (TextView) v.findViewById(R.id.text_engine_num);

        mVincode = (TextView) v.findViewById(R.id.text_vincode);

        car_state = (LinearLayout) v.findViewById(R.id.lin_car_state);

        mReTime = (TextView) v.findViewById(R.id.text_re_time);

        (add_cost = (TextView) v.findViewById(R.id.tv_add_cost)).setOnClickListener(this);

        lv_car_cost = (MyListView) v.findViewById(R.id.lv_car_cost);

        mExpAdapter = new ExpenseDetailsAdapter(getContext(), mExpList);

        lv_car_cost.setAdapter(mExpAdapter);

        getExpenseDetail.execute(getContext());

        (cost_detail = (TextView) v.findViewById(R.id.tv_car_cost_detail)).setOnClickListener(this);

        cost_detail.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线

        cost_detail.getPaint().setAntiAlias(true);//抗锯齿

        mMiles = (TextView) v.findViewById(R.id.text_miles);

        mMaintainPeriod = (TextView) v.findViewById(R.id.text_maintain_period);

        mBatteryState = (TextView) v.findViewById(R.id.text_battery_state);

        mDetection = (TextView) v.findViewById(R.id.btn_detection);

        mLinBattery = (LinearLayout) v.findViewById(R.id.lin_battery);

        mLinMaintain = (LinearLayout) v.findViewById(R.id.lin_maintain);

        mLinPeccancy = (LinearLayout) v.findViewById(R.id.lin_peccancy);

        mLinClearError = (LinearLayout) v.findViewById(R.id.lin_clear_error);

        iv_car_battery = (ImageView) v.findViewById(R.id.iv_car_battery);

        iv_car_clear_error = (ImageView) v.findViewById(R.id.iv_car_clear_error);

        getMaintainRemind.execute(getContext());

        mCarNameText.setText(JTApplication.jtPreferences.readPP());

        mCarNumText.setText(JTApplication.jtPreferences.readPlateNum());

        mCarInfoText.setText(JTApplication.jtPreferences.readCX() + JTApplication.jtPreferences.readNK());

        mEngineNum.setText("发动机号 : " + JTApplication.jtPreferences.readEngineCode());

        mVincode.setText("车架号 : " + JTApplication.jtPreferences.readVincode());

        mReTime.setText("注册时间 : " + JTApplication.jtPreferences.readRemark());

        mLocationImg.setOnClickListener(this);
        mEditCarInfo.setOnClickListener(this);
        mDetection.setOnClickListener(this);
        mLinBattery.setOnClickListener(this);
        mLinMaintain.setOnClickListener(this);
        mLinPeccancy.setOnClickListener(this);
        mLinClearError.setOnClickListener(this);

        if (!account_state.equals("2")) {

            car_state.setVisibility(View.GONE);

            iv_car_battery.setImageResource(R.mipmap.icon_battery_off);

            iv_car_clear_error.setImageResource(R.mipmap.icon_clear_error_off);

        }

        refreshLinster = new RefreshLinster() {
            @Override
            public void refresh() {

                mCarNameText.setText(JTApplication.jtPreferences.readPP());

                mCarNumText.setText(JTApplication.jtPreferences.readPlateNum());

                mCarInfoText.setText(JTApplication.jtPreferences.readCX() + JTApplication.jtPreferences.readNK());

                mEngineNum.setText("发动机号 : " + JTApplication.jtPreferences.readEngineCode());

                mVincode.setText("车架号 : " + JTApplication.jtPreferences.readVincode());

                mReTime.setText("注册时间 : " + JTApplication.jtPreferences.readRemark());

                getExpenseDetail.execute(getContext());

                getMaintainRemind.execute(getContext());

            }
        };

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.icon_location:

                if (account_state.equals("2")) {

                    getCarGps.execute(getActivity());

                } else {

                    UtilToast.show(getContext(), "该功能需要绑定OBD设备");

                }

                break;

            case R.id.text_editcarinfo:

                startActivity(new Intent(getActivity(), CarInfoActivity.class));

                break;

            case R.id.btn_detection:

                //                Intent intent = new Intent(getActivity(), DiagnosisActivity.class);
//                startActivity(intent);

//                  Intent intent_det = new Intent(getActivity(), DetectionResultActivity.class);
//                  startActivity(intent_det);

                Intent intent_detre = new Intent(getActivity(), DetectionActivity.class);
                startActivity(intent_detre);

                break;

            case R.id.lin_battery:

                if (account_state.equals("2")) {

                    Intent intent_battery = new Intent(getActivity(), BatteryStateActivity.class);
                    startActivity(intent_battery);

                } else {

                    UtilToast.show(getContext(), "该功能需要绑定OBD设备");

                }

                break;

            case R.id.lin_maintain:

                Intent intent_maintain_set = new Intent(getActivity(), MaintaenanceSettingActivity.class);
                startActivity(intent_maintain_set);

                break;

            case R.id.lin_peccancy:

                Intent intent_illegal = new Intent(getActivity(), IllegalQueryActivity.class);
                startActivity(intent_illegal);

                break;

            case R.id.lin_clear_error:

                if (account_state.equals("2")) {

                    Intent intent_error = new Intent(getActivity(), ErrorCodeActivity.class);

                    startActivity(intent_error);

                } else {

                    UtilToast.show(getContext(), "该功能需要绑定OBD设备");

                }

                break;

            case R.id.tv_car_cost_detail:

                startActivity(new Intent(getActivity(), ExpenseDetailActivity.class));

                break;

            case R.id.tv_add_cost:

                AddExpenseDialog dialog = new AddExpenseDialog(getContext());

                dialog.show();

                break;


        }

    }

    public abstract class RefreshLinster {

        public abstract void refresh();

    }

}
